package sketch.specs;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;

import sketch.ast.ASTUtils;
import sketch.specs.annotation.AnnotationProcessor;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class MethodrepInstanitiationsTest extends TestCase {
	
	private String className = "sketch.specs.annotation.ClassUseRepTest";
	private String filePath = "./testfiles/sketch/specs/annotation/ClassUseRepTest.java";
	
	public static Test suite() {
		return new TestSuite(MethodrepInstanitiationsTest.class);
	}
	
	public void testInstantiate() throws ClassNotFoundException {
		Class<?> clz = Class.forName(className);
		AnnotationProcessor processor = new AnnotationProcessor(clz);
		processor.process();
		
		List<MethodDeclaration> methods = ASTUtils.getAllMethods(ASTUtils.astRoot(filePath));
		
		for(Method ms : clz.getDeclaredMethods()) {
			List<Map<String, String>> mapping_list = processor.getMappings(ms);
			MethodDeclaration m = this.getMethodDeclaration(ms, methods);
			if( mapping_list == null) {
				continue;
			}
			if(m == null) {
				continue;
			}
			MethodRepInstanitiations instanitiation = new MethodRepInstanitiations(m, mapping_list);
			List<MethodDeclaration> method_declares = instanitiation.getReplacedMethods();
			for(MethodDeclaration method_declare : method_declares) {
			 // method_declare.getName().setIdentifier(method_declare.getName().getIdentifier() + "_replaced");
			  System.out.println(method_declare);
			}
		}
		System.out.println("----------------------------");
	}
	
	public void testInstantiate2() {
		System.out.println(MethodRepInstanitiations.instantiate(className, filePath));
	}
	
	
	public void testGetNewCompilationUnitWithReplicateMethods() {
		String testFile = "./testfiles/sketch/specs/annotation/ClassUseRepTest.java";
		CompilationUnit unit = ASTUtils.astRoot(testFile);
		CompilationUnit newUnit = MethodRepInstanitiations.getCompilationUnit("sketch.specs.annotation.ClassUseRepTest", testFile);
		System.out.println(newUnit);
	}
	
	MethodDeclaration getMethodDeclaration(Method method, List<MethodDeclaration> methods) {
//		System.out.println("----");
		for(MethodDeclaration m : methods) {
//			System.out.println(m.getName());
//			System.out.println(method.getName());
			if(m.getName().toString().equals(method.getName().toString())) {
				return m;
			}
		}
		return null;
	}
}
